package com.util;

/**
 * @author sschina-tech2
 *
 */
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期转换相关函数
 */
public class DateUtil {
	public static final String yyyy_MM_dd = "yyyy-MM-dd";

	public static Date stringToDate(String dateText, String format) {

		if (dateText == null || dateText.equals("")) {
			return null;
		}
		DateFormat df;
		try {
			if (format == null || format.equals("")) {
				df = new SimpleDateFormat(yyyy_MM_dd);
			} else {
				df = new SimpleDateFormat(format);
			}
			df.setLenient(false);
			return df.parse(dateText);
		} catch (ParseException e) {
			System.out.println("错误：" + e);
			return null;
		}
	}

	public static String getCurrentDateString() {
		return dateToString(getCurrentDateTime(), yyyy_MM_dd);
	}

	public static String dateToString(Date date, String pattern) {

		if (date == null) {
			return null;
		}

		try {
			SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
			sfDate.setLenient(false);
			return sfDate.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public static String fmtDate(Date date) {
		if (date == null) {
			return "";
		}

		try {
			SimpleDateFormat sfDate = new SimpleDateFormat(yyyy_MM_dd);
			sfDate.setLenient(false);
			return sfDate.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public static Date stringToDate(String dateText) {
		return stringToDate(dateText, null);
	}

	/**
	 * 取当前日期时间
	 * 
	 * @return
	 */
	public static Date getCurrentDateTime() {
		java.util.Calendar calNow = java.util.Calendar.getInstance();
		java.util.Date dtNow = calNow.getTime();
		return dtNow;
	}

	/**
	 * 本年
	 * 
	 * @return
	 */
	public static String getThisYear() {

		return dateToString(getCurrentDateTime(), "yyyy");
	}

	/**
	 * 本月
	 * 
	 * @return
	 */
	public static String getThisMonth() {

		return dateToString(getCurrentDateTime(), "M");
	}

	/**
	 * 本月，汉字
	 * 
	 * @return
	 */
	public static String getThisMonthChar() {
		String str = dateToString(getCurrentDateTime(), "M");
		String charMonth = "";
		if (str.equals("1")) {
			charMonth = "一月";
		} else if (str.equals("2")) {
			charMonth = "二月";
		} else if (str.equals("3")) {
			charMonth = "三月";
		} else if (str.equals("4")) {
			charMonth = "四月";
		} else if (str.equals("5")) {
			charMonth = "五月";
		} else if (str.equals("6")) {
			charMonth = "六月";
		} else if (str.equals("7")) {
			charMonth = "七月";
		} else if (str.equals("8")) {
			charMonth = "八月";
		} else if (str.equals("9")) {
			charMonth = "九月";
		} else if (str.equals("10")) {
			charMonth = "十月";
		} else if (str.equals("11")) {
			charMonth = "十一月";
		} else if (str.equals("12")) {
			charMonth = "十二月";
		}

		return charMonth;
	}

	public static void main(String[] args) {
		// Calendar calendar = Calendar.getInstance();

		// System.out.println("sunday: " + Calendar.SUNDAY);
		// System.out.println("monday: " + Calendar.MONDAY);
		// System.out.println("tuesday: " + Calendar.TUESDAY);
		// System.out.println("wendesday: " + Calendar.WEDNESDAY);
		// System.out.println("thursday: " + Calendar.THURSDAY);
		// System.out.println("friday: " + Calendar.FRIDAY);
		// System.out.println("saturday: " + Calendar.SATURDAY);
		System.out.println("saturday: " + getThisMonth());
	}
}
